/* COPYRIGHT_CHUNFENG */
#include "cf_slist.h"
#include "cf_std.h"
CFSListNode* cf_slist_prepend(CFSListNode* head, void* data)
{
	CFSListNode* newNode = malloc(sizeof(CFSListNode));
		newNode->data = data;
	if(head == NULL) {
		newNode->next = NULL;
	}else{
		newNode->next = head;
	}
	return newNode;
}

CFSListNode* cf_slist_append(CFSListNode* head, void* data)
{
	CFSListNode* newNode = malloc(sizeof(CFSListNode));
	newNode->data = data;
	newNode->next =NULL;	
	if(head == NULL) {
		return newNode;
	}else{
		CFSListNode* tmpNode = head;
		for(; cf_slist_get_next(tmpNode); tmpNode = cf_slist_get_next(tmpNode)){}
		tmpNode->next = newNode;
		return head;
	}	
}

CFSListNode* cf_slist_del(CFSListNode* head, void* data_to_del)
{
	CFSListNode* prev = NULL;
	CFSListNode* tmpNode = head;
	while(tmpNode){
		if(tmpNode->data == data_to_del){
			if(!prev){
				return tmpNode->next;
			}else{
				prev->next = tmpNode->next;
				return head;
			}
		}
		prev = tmpNode;
		tmpNode = tmpNode->next;
	}
	return head;
}

CFSListNode* cf_slist_reverse(CFSListNode* head)
{
	CFSListNode* prev = NULL;
	while(head){
		CFSListNode* next;
		next = head ->next;
		head->next = prev;
		prev = head;
		head = next;
	}
	return prev;
}
void cf_slist_free(CFSListNode* head)
{
	while(head != NULL){
		CFSListNode* tmp = cf_slist_get_next(head);
		free(head);
		head = tmp;
	}
}

void cf_slist_free_all(CFSListNode* head, CFFreeObjFunc freeFunc)
{
	while(head != NULL){
		CFSListNode* tmp = cf_slist_get_next(head);
		freeFunc(head->data);
		free(head);
		head = tmp;
	}
}
